package com.newtype.meeting.client.widget.frontend;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;
import com.newtype.meeting.client.model.MeetingNotify;
import com.newtype.meeting.client.widget.SubjectHtml;

/**
 * 会议通知页面
 * @author 汪恭泽
 *
 */
public class MeetingNoticePanel extends ContentPanel {
	HtmlEditor replyContent;
	RadioGroup group;

	public MeetingNoticePanel(Meeting meeting, MeetingNotify notify) {
		setHeading("会议通知");
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTO);

		createContentPanel(meeting, notify);
	}

	private void createContentPanel(Meeting meeting, MeetingNotify notify) {
		DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");

		FormData formData = new FormData("100%");
		FormPanel panel = new FormPanel();
		panel.setFrame(true);
		panel.setHeaderVisible(false);
		panel.setLabelAlign(LabelAlign.TOP);
		panel.setButtonAlign(HorizontalAlignment.CENTER);
		panel.setStyleAttribute("margin", "10px 200px 10px 200px");

		String imgsrc = "images/message.png";
		SubjectHtml subjectHtml = new SubjectHtml("会议通知", imgsrc, "", "", "");
		panel.add(subjectHtml);

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		left.setLayout(layout);

		String str = "&nbsp;&nbsp;&nbsp;&nbsp;";

		LabelField category = new LabelField();
		category.setFieldLabel("<b>类别:</b>");
		category.setValue(str + meeting.getCategoryName());
		left.add(category, formData);

		LabelField subject = new LabelField();
		subject.setFieldLabel("<br/><b>主题:</b>");
		subject.setValue(str + meeting.getSubject());
		left.add(subject, formData);

		LabelField date = new LabelField();
		date.setFieldLabel("<br/><b>会议日期:</b>");
		date.setValue(str + dtf.format(meeting.getStartDate()));
		left.add(date, formData);

		LabelField chair = new LabelField();
		chair.setFieldLabel("<br/><b>会议主席:</b>");
		chair.setValue(str + meeting.getChairName());
		left.add(chair, formData);

		LabelField invited = new LabelField();
		invited.setFieldLabel("<br/><b>与会人员:</b>");
		invited.setValue(str + meeting.getInvitedName());
		left.add(invited, formData);

		LabelField guidance = new LabelField();
		guidance.setFieldLabel("<br/><b>外部指导人员:</b>");
		String guidanceStr = "";
		if (meeting.getGuidance() != null) {
			guidanceStr = meeting.getGuidance();
		}
		guidance.setValue(str + guidanceStr);
		left.add(guidance, formData);

		LayoutContainer right = new LayoutContainer();
		right.setStyleAttribute("paddingLeft", "10px");
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);

		LabelField originator = new LabelField();
		originator.setFieldLabel("<b>发起人:</b>");
		originator.setValue(str + meeting.getOriginatorName());
		right.add(originator, formData);

		LabelField location = new LabelField();
		location.setFieldLabel("<br/><b>会议地点:</b>");
		location.setValue(str + meeting.getLocation());
		right.add(location, formData);

		LabelField time = new LabelField();
		time.setFieldLabel("<br/><b>会议时间:</b>");

		time
				.setValue(str + meeting.getStartTime() + "～"
						+ meeting.getEndTime());
		right.add(time, formData);

		LabelField recorder = new LabelField();
		recorder.setFieldLabel("<br/><b>会议记录:</b>");
		recorder.setValue(str + meeting.getRecorderName());
		right.add(recorder, formData);

		LabelField invitedOthers = new LabelField();
		invitedOthers.setFieldLabel("<br/><b>列席人员:</b>");
		String others = "";
		if (meeting.getInvitedOthersName() != null) {
			others = meeting.getInvitedOthersName();
		}
		invitedOthers.setValue(str + others);
		right.add(invitedOthers, formData);

		main.add(left, new ColumnData(.5));
		main.add(right, new ColumnData(.5));

		panel.add(main, formData);

		LabelField content = new LabelField();
		content.setFieldLabel("<br/><b>议程：</b>");
		content.setValue(str + meeting.getContent());
		panel.add(content, formData);

		replyContent = new HtmlEditor();
		replyContent.setFieldLabel("<br/><hr size='1'><b>回复内容</b>");
		replyContent.setHeight(200);
		panel.add(replyContent, new FormData("98%"));

		Radio radio1 = new Radio();
		radio1.setBoxLabel("参加本次会议");
		radio1.setValue(true);
		radio1.setValueAttribute("1");

		Radio radio2 = new Radio();
		radio2.setBoxLabel("不参加本次会议");
		radio2.setValueAttribute("0");

		group = new RadioGroup();
		group.setFieldLabel("<b>是否参加</b>");
		group.add(radio1);
		group.add(radio2);
		panel.add(group);

		panel.addButton(getReplyButton(notify));
		add(panel);
	}

	private Button getReplyButton(final MeetingNotify notify) {
		Button button = new Button("回复此会议通知");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				notify.setAttendTAG(Integer.parseInt(group.getValue()
						.getValueAttribute()));
				notify.setReplyContent(replyContent.getValue());
				Dispatcher.forwardEvent(AppEvents.ReplyMeetingNotify, notify);
			}
		});
		return button;
	}
}
